#include<iostream>
#include<string>
#include<fstream>
#include<windows.h>

#include"Debug.h"

namespace Debug
{
	namespace 
	{
		bool die_on_error = false;
		bool abort_on_die = false;
		const std::string error_output_file_name = "log/error.txt";
		const std::string log_file_name = "log/log.txt";

		std::ofstream error_output_file;
		std::ofstream log_output_file;
	}

	CFatal::CFatal(const std::string& error_message)
	{

	}
	
	const char* CFatal::what()const
	{
		return m_error_message.c_str();
	}

	void check_assertion(bool assertion, const std::string& error_message)
	{
		if (!assertion)
		{
			die(error_message);
		}
	}


	void die(const std::string& error_message)
	{
		if (!error_output_file.is_open())
		{
			error_output_file.open(error_output_file_name.c_str());
		}

		error_output_file << "Fatal: " << error_message << std::endl;
		std::cerr << "Fatal: " << error_message << std::endl;

		if (abort_on_die)
		{
			std::abort();
		}
		else
		{
			throw CFatal(error_message);
		}
	}

	void log(const std::string& log_msg, ...)
	{
		va_list args;
		va_start(args, log_msg);
		log_output(log_msg.c_str(), args);
		va_end(args);
	}

	void log_output(const char* format, va_list args)
	{
		if (!log_output_file.is_open())
		{
			log_output_file.open(log_file_name.c_str());
		}

		char buffer[512];
		vsprintf(buffer, format, args);

		std::cout << buffer << std::endl;
		log_output_file << buffer << std::endl;
	}

}